clear
est drop _all

* mainpath should be set to the Replication package base directory
global mainpath ".."

* MMS
import excel "$mainpath\data\macro_news_updated.xlsx", sheet("Sheet1") firstrow clear
rename DATE Date
destring *, replace force
*append using `tmp'
tempfile tmp
save `tmp', replace

import delimited "$mainpath\data\feds200628.csv", varnames(10) stringcols(1)  case(preserve) clear
gen d=date(Date,"YMD")
drop Date
rename d Date
destring SVENY* SVENF*, force replace 
keep Date SVENY01 SVENY02 SVENY05 SVENY10 SVENF05 SVENF09 SVENF10
merge 1:1 Date using `tmp'
drop _merge
keep if Date >= mdy(1,1,1990)
save `tmp', replace

import excel "$mainpath\data\ff futures updated.xlsx", sheet("Yields") firstrow clear
destring FF*, force replace
renvars FF* , postsub("Comdty" "Yield")
rename Dates Date
merge 1:1 Date using `tmp'
drop _merge
keep if Date >= mdy(1,1,1990)
save `tmp', replace

import delimited "$mainpath\output\bluechip_rule_regressions.csv", varnames(1) clear 
gen ym = mofd(date(date,"YMD"))
format ym %tm
order ym
tsset ym
tempfile gamma_regs
save `gamma_regs'

import delimited "$mainpath\output\bluechip_rule_inertial.csv", varnames(1) clear 
gen ym = mofd(date(date,"YMD"))
format ym %tm
order ym
tsset ym
rename gamma gamma_fe_inertial
tempfile gamma_ssm
save `gamma_ssm'

use `tmp', clear
gen ym = mofd(Date)
merge m:1 ym using `gamma_regs'
drop _merge
merge m:1 ym using `gamma_ssm'
drop _merge
drop if mi(Date)

gen cap_util_sur = CAPAREL- CAPAEXP
gen cons_conf_sur = CCONFREL- CCONFEXP
gen core_cpi_sur = CPIXFEREL- CPIXFEEXP
gen gdp_adv_sur = GDPADVREL- GDPADVEXP
gen init_claims_sur = ICLMREL- ICLMEXP
gen ism_manu_sur = NAPMREL- NAPMEXP
gen lead_ind_sur = LDERSREL- LDERSEXP
gen home_sales_sur = NHOMESREL- NHOMESEXP
gen nonfarm_pay_sur = NFPAYREL- NFPAYEXP
gen core_ppi_sur = PPIXFEREL- PPIXFEEXP
gen retail_exauto_sur = RSXAUTREL- RSXAUTEXP
gen unemp_sur = UNEMPREL- UNEMPEXP
sum *_sur

foreach var of varlist *_sur {
	sum `var'
	gen `var'_sd =`var'/ `r(sd)'
	replace `var'_sd = 0 if mi(`var')
}
*foreach var of varlist SVENY* FF*Yield {
*	replace `var' = `var'*100
*}

gen announce_date = ~mi(CAPAREL) | ~mi(CCONFREL) | ~mi(CPIXFEREL) | ~mi(GDPADVREL) | ~mi(ICLMREL) | ~mi(NAPMREL) | ~mi(LDERSREL) | ~mi(NHOMESREL) | ~mi(NFPAYREL) | ~mi(PPIXFEREL) | ~mi(RSXAUTREL) | ~mi(UNEMPREL)
tab announce_date

* set up daily data, fix Mondays
tsset Date
gen s_SVENY02 = s.SVENY02
replace s_SVENY02 = s3.SVENY02 if mi(l.SVENY02) & mod(Date,7)==3
gen s_SVENY10 = s.SVENY10
replace s_SVENY10 = s3.SVENY10 if mi(l.SVENY10) & mod(Date,7)==3

* check that macro announcements move yields, as in Swanson-Williams Table 1
reg s_SVENY02 *_sur_sd if inrange(Date,mdy(1,1,1990),mdy(12,31,2000)) & announce_date , ro
reg s_SVENY10 *_sur_sd if inrange(Date,mdy(1,1,1990),mdy(12,31,2000)) & announce_date , ro

forvalues k=1/24 {
	gen s_FF`k'Yield = s.FF`k'Yield
	replace s_FF`k'Yield = s3.FF`k'Yield if mi(l.SVENY02) & mod(Date,7)==3
	replace s_FF`k'Yield = . if mi(s_SVENY02)
}

reg s_SVENY10  *_sur_sd if announce_date, ro
predict xb, xb
gen gamma_fe_X_xb = xb*gamma_fe
reg s_SVENY10 xb gamma_fe gamma_fe_X_xb if announce_date , ro
gen sample = e(sample)
drop xb gamma_fe_X_xb

gen gamma = .
gen xb = .
gen Z = .
gen gamma_X_Z = .


local i 0
foreach gvar of varlist gamma_fe {
	replace gamma = `gvar'
	
	foreach zvar of varlist nonfarm_pay_sur_sd xb {	
		
		foreach yvar of varlist s_FF3Yield s_FF6Yield s_SVENY02 s_SVENY10 {
			drop xb
			qui reg `yvar' *_sur_sd if announce_date & sample, ro
			predict xb, xb

			replace Z = `zvar'
			replace gamma_X_Z = gamma*Z
				
			reg `yvar' gamma Z gamma_X_Z if announce_date  & sample, ro
			est store m`i++'
		}
	}
}
esttab m* using "$mainpath\tables\macro_news_a.tex", star(* .10 ** .05 *** .01)  b(3) se(3) nomtitle replace r2(2) noobs

est drop _all
local i 0
foreach gvar of varlist gamma_fe_inertial {
	replace gamma = `gvar'
	
foreach zvar of varlist nonfarm_pay_sur_sd xb {	
		
		foreach yvar of varlist s_FF3Yield s_FF6Yield s_SVENY02 s_SVENY10 {
			drop xb
			qui reg `yvar' *_sur_sd if announce_date  & sample, ro
			predict xb, xb

			replace Z = `zvar'
			replace gamma_X_Z = gamma*Z
				
			reg `yvar' gamma Z gamma_X_Z if announce_date  & sample, ro
			est store m`i++'
		}
	}
}
esttab m* using "$mainpath\tables\macro_news_b.tex", star(* .10 ** .05 *** .01)  b(3) se(3) nomtitle replace r2(2) noobs
